package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.hd.trans.R;
import com.hd.trans.framework.click.SingleClick;
import com.hd.trans.utils.StringUtils;


/**
 * 通用提示弹框
 *
 * @author yang
 * created at 2018/6/25 10:35
 */
public class TipsNormalDlg extends BaseHdDlg implements View.OnClickListener {

    TextView tvContent;
    TextView tvConfirm;
    TextView tvCancel;


    private static final String ARG_TIPS_DATA = "arg_tips_data";


    public static TipsNormalDlg newInstance(TipsDlgData tipsData) {
        TipsNormalDlg instance = new TipsNormalDlg();
        Bundle args = new Bundle();
        args.putParcelable(ARG_TIPS_DATA, tipsData);
        instance.setArguments(args);
        return instance;
    }


    protected int getAnimStyle() {
        return R.style.BaseDialogStyle;
    }

    @Override
    protected void initDialog(Dialog dialog) {
        setDialogGravity(Gravity.CENTER);
        View view = inflateContentView(R.layout.dialog_tips_common);
        tvContent = view.findViewById(R.id.tv_content);
        tvConfirm = view.findViewById(R.id.tv_confirm);
        tvCancel = view.findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        Bundle args = getArguments();
        if (args != null) {
            TipsDlgData tipsData = args.getParcelable(ARG_TIPS_DATA);
            if (tipsData != null) {
                if (!StringUtils.isEmpty(tipsData.content)) {
                    tvContent.setText(tipsData.content);
                }
                if (!StringUtils.isEmpty(tipsData.okStr)) {
                    tvConfirm.setText(tipsData.okStr);
                }
                if (!StringUtils.isEmpty(tipsData.cancelStr)) {
                    tvCancel.setText(tipsData.cancelStr);
                }
                tvCancel.setVisibility(tipsData.needCancel ? View.VISIBLE : View.GONE);
                setCancelable(tipsData.cancelable);
                setCanceledOnTouchOutside(tipsData.canceledOnTouchOutside);
            }

        }
        dialog.setContentView(view);
    }

    public void setTvContent(String content){
        tvContent.setText(content);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        if(dialog != null && dialog.isShowing()){
            return;
        }
        super.show(manager, tag);
    }

    @SingleClick
    @Override
    public void onClick(View v) {
       /* switch (v.getId()) {
            case R.id.tv_confirm:
                dismiss();
                if (positiveClickListener != null)
                    positiveClickListener.onPositiveClick();
                break;

            case R.id.tv_cancel:
                dismiss();
                if (negativeClickListener != null)
                    negativeClickListener.onNegativeClick();
                break;
        }*/
        if(v.getId() == R.id.tv_confirm) {
            dismiss();
            if (positiveClickListener != null)
                positiveClickListener.onPositiveClick();
        }else if(v.getId() == R.id.tv_cancel){
            dismiss();
            if (negativeClickListener != null)
                negativeClickListener.onNegativeClick();
        }
    }


    /**
     * 提示框传递数据
     */
    public static class TipsDlgData implements Parcelable {

        private String content;
        String okStr;
        String cancelStr;
        boolean canceledOnTouchOutside;
        boolean cancelable;
        boolean needCancel;

        private TipsDlgData(Builder builder) {
            content = builder.content;
            okStr = builder.okStr;
            cancelStr = builder.cancelStr;
            canceledOnTouchOutside = builder.canceledOnTouchOutside;
            cancelable = builder.cancelable;
            needCancel = builder.needCancel;
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.content);
            dest.writeString(this.okStr);
            dest.writeString(this.cancelStr);
            dest.writeByte(this.canceledOnTouchOutside ? (byte) 1 : (byte) 0);
            dest.writeByte(this.cancelable ? (byte) 1 : (byte) 0);
            dest.writeByte(this.needCancel ? (byte) 1 : (byte) 0);
        }

        protected TipsDlgData(Parcel in) {
            this.content = in.readString();
            this.okStr = in.readString();
            this.cancelStr = in.readString();
            this.canceledOnTouchOutside = in.readByte() != 0;
            this.cancelable = in.readByte() != 0;
            this.needCancel = in.readByte() != 0;
        }

        public static final Creator<TipsDlgData> CREATOR = new Creator<TipsDlgData>() {
            @Override
            public TipsDlgData createFromParcel(Parcel source) {
                return new TipsDlgData(source);
            }

            @Override
            public TipsDlgData[] newArray(int size) {
                return new TipsDlgData[size];
            }
        };


        public static final class Builder {
            private String content;
            private String okStr;
            private String cancelStr;
            private boolean canceledOnTouchOutside = true;
            private boolean cancelable = true;
            private boolean needCancel = true;

            public Builder() {
            }

            public Builder content(String val) {
                content = val;
                return this;
            }

            public Builder okStr(String val) {
                okStr = val;
                return this;
            }

            public Builder cancelStr(String val) {
                cancelStr = val;
                return this;
            }

            public Builder canceledOnTouchOutside(boolean val) {
                canceledOnTouchOutside = val;
                return this;
            }

            public Builder cancelable(boolean val) {
                cancelable = val;
                return this;
            }

            public Builder needCancel(boolean val) {
                needCancel = val;
                return this;
            }

            public TipsDlgData build() {
                return new TipsDlgData(this);
            }
        }
    }


}
